package com.gulimall.cart.vo;

import lombok.*;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description
 * @Author lhj
 * @Date 2024/3/16 11:56
 */
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
public class Cart implements Serializable {

    private static final long serialVersionUID = 42L;
    
    private List<CartItem> cartItems;

    /**
     * 购物类型
     */
    private Integer cartType = 0;

    /**
     * 购物车数量
     */
    private Integer cartNumber;

    /**
     * 总金额
     */
    private BigDecimal totalAmount;

    /**
     * 优惠信息
     */
    private BigDecimal reduce = new BigDecimal("0.00");


    public List<CartItem> getCartItems() {
        return cartItems;
    }

    public void setCartItems(List<CartItem> cartItems) {
        this.cartItems = cartItems;
    }

    public Integer getCartType() {
        AtomicInteger cartTypeAtomic = new AtomicInteger(cartType);
        if (!CollectionUtils.isEmpty(cartItems)) {
            cartItems.forEach(item -> {
                cartTypeAtomic.addAndGet(1);
            });
        }
        return cartTypeAtomic.get();
    }
    

    public Integer getCartNumber() {
        AtomicInteger cartNumberAtomic = new AtomicInteger();
        if (!CollectionUtils.isEmpty(cartItems)) {
            cartItems.forEach(item -> {
                Integer count = item.getCount();
                cartNumberAtomic.addAndGet(count);
            });
        }
        return cartNumberAtomic.get();
    }
    

    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0.0");
        if (!CollectionUtils.isEmpty(cartItems)) {
            for (CartItem cartItem : cartItems) {
                if (cartItem.getCheck()) {
                    amount = amount.add(cartItem.getTotalPrice());
                }
            }
        }

        amount.subtract(getReduce());
        return amount;
    }
    

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
